package com.kaishu.geoquiz;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class CheatActivity extends ActionBarActivity {

    public static final String EXTRA_ANSWER_IS_TRUE = "com.kaishu.geoquiz.answer_is_true";
    public static final String EXTRA_ANSWER_SHOWN = "com.kaishu.geoquiz.answer_shown";
    private static final String KEY_ANSWER_IS_SHOW = "isShow";
    private static final String KEY_ANSWER_TEXT_VIEW = "text";

    private boolean mAnswerIsTrue;
    private boolean mAnswerIsShown;

    private TextView mAnswerTextView;
    private Button mShowAnswer;
    private TextView mApiLevelTextView;

    private void setAnswerShownResult(boolean isAnswerShown){
        Intent data = new Intent();
        data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
        setResult(RESULT_OK, data);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cheat);

        mAnswerTextView = (TextView) findViewById(R.id.answerTextView);

        if(savedInstanceState != null){
            mAnswerIsShown = savedInstanceState.getBoolean(KEY_ANSWER_IS_SHOW, false);
            mAnswerTextView.setText(savedInstanceState.getString(KEY_ANSWER_TEXT_VIEW));
        }
        setAnswerShownResult(mAnswerIsShown);

        mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);

        mShowAnswer = (Button) findViewById(R.id.showAnswerButton);
        mShowAnswer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mAnswerIsTrue){
                    mAnswerTextView.setText(R.string.true_button);
                }else{
                    mAnswerTextView.setText(R.string.false_button);
                }
                mAnswerIsShown = true;
                setAnswerShownResult(true);
            }
        });

        mApiLevelTextView = (TextView) findViewById(R.id.apiLevelTextView);
        int version = Integer.valueOf(android.os.Build.VERSION.SDK);
        mApiLevelTextView.setText("API level " + String.valueOf(version));
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putBoolean(KEY_ANSWER_IS_SHOW, mAnswerIsShown);
        savedInstanceState.putString(KEY_ANSWER_TEXT_VIEW, mAnswerTextView.getText().toString());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_cheat_avtivity, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
